package com.wcar.frontsso.realm;

import com.wcar.common.entity.FrontUser;
import com.wcar.common.vo.Result;
import com.wcar.frontsso.feign.FrontUserFeign;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

public class MyFrontRealm extends AuthorizingRealm {
    @Resource
    private FrontUserFeign userFeign;
    //sso只负责登录认证 不负责数据查询
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //1.获取账号
        String name = (String) token.getPrincipal();
        //2.根据账号查询用户信息
        Result<FrontUser> result = userFeign.fontSelectByName(name);
        if(result.getCode()!=500){
            FrontUser frontUser = result.getData();
            ByteSource salt=ByteSource.Util.bytes(frontUser.getSalt());
            SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(frontUser,frontUser.getPassword(),salt,this.getName());
            return info;
        }
        return null;
    }
}